package com.wjf.birdfish.util;

import java.text.*;
import java.util.*;

import org.apache.commons.lang.StringUtils;

/**
 * @author tongys 2005-11-27  
 * 
 * 日期帮助类
 */
public class TimeHelper {
	private static String CurrentTime;
	private static String CurrentDate;
	
	
	public static String getCurrentDateTimeForFormat(String format) {
		java.util.Date NowDate = new java.util.Date();

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(NowDate);
	}
	
	/**
	 * 得到当前的年份
	 * 返回格式:yyyy
	 * @return String
	 */
	public static String getCurrentYear() {
		java.util.Date NowDate = new java.util.Date();

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
		return formatter.format(NowDate);
	}
	
	public static String formatDate(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}
	
	/**
	 * 得到当前的月份
	 * 返回格式:MM
	 * @return String
	 */
	public static String getCurrentMonth() {
		java.util.Date NowDate = new java.util.Date();

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
		return formatter.format(NowDate);
	}
	/**
	 * 得到当前的日期
	 * 返回格式:dd
	 * @return String
	 */
	public static String getCurrentDay() {
		java.util.Date NowDate = new java.util.Date();

		SimpleDateFormat formatter = new SimpleDateFormat("dd");
		return formatter.format(NowDate);
	}
	/**
	 * 得到当前的时间，精确到毫秒,共19位
	 * 返回格式:yyyy-MM-dd:HH:mm:ss
	 * @return String
	 */
	public static String getCurrentTime() {
		Date NowDate = new Date();
		SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}
	
	public static String getCurrentTime3() { 
		Date NowDate = new Date();
		SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}
	
	/**
	 * 得到当前的时间，精确到毫秒,共19位
	 * 返回格式:yyyy-MM-dd:HH:mm:ss
	 * @return String
	 */
	public static String getCurrentTimeSerisl() {
		Date NowDate = new Date();
		SimpleDateFormat formatter =new SimpleDateFormat("yyyyMMddHHmmss");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}
	
	/**
	 * 得到当前的时间，精确到毫秒,共19位
	 * 返回格式:yyyy-MM-dd:HH:mm:ss
	 * @return String
	 */
	public static String getCurrentTime8() {
		Date NowDate = new Date();
		SimpleDateFormat formatter =new SimpleDateFormat("yyyyMMdd");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}
	/**
	 * 得到当前的时间，精确到毫秒,共19位
	 * 返回格式:yyyy-MM-dd:HH:mm:ss
	 * @return String
	 */
	public static String getCurrentTimeForId() {
		Date NowDate = new Date();
		SimpleDateFormat formatter =new SimpleDateFormat("yyyyMMddHH");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}
	/**
	 * 得到当前的时间，精确到毫秒,共19位
	 * 返回格式:yyyy-MM-dd HH:mm:ss
	 * @return String
	 */
	public static String getCurrentTime2() {
		Date NowDate = new Date();
		SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}
	/**
	 * 得到当前的时间，精确到毫秒,共19位
	 * 返回格式:yyyy-MM-dd HH:mm:ss
	 * @return String
	 */
	public static String getStartTime(String sysDate) {
		StringBuffer cTime=new StringBuffer();
		String minutes=null;
		if(StringUtils.isNotBlank(sysDate)){
			minutes=sysDate.substring(15, 16);
			if(Integer.parseInt(minutes)>=5){
			  cTime.append(sysDate.substring(0, 15)).append("5:00");
			}else
			  cTime.append(sysDate.substring(0, 15)).append("0:00");
		}
		return cTime.toString();
	}
	/**
	 * 得到当前的时间，精确到毫秒,共19位
	 * 返回格式:yyyy-MM-dd HH:mm:ss
	 * @return String
	 */
	public static String getEndTime(String sysDate) {//addMinutes
		String startTime=TimeHelper.getStartTime(sysDate);
		System.out.println("startTime="+startTime);
		return TimeHelper.addMinutes(startTime, 5);
	}
	/**
	 * 得到当前的时间，精确到毫秒,共19位
	 * 返回格式:yyyy-MM-dd HH:mm:ss
	 * @return String
	 */
	public static String getCurrentTimeForMonth() {
		Date NowDate = new Date();
		SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-01 00:00:00");
		CurrentTime = formatter.format(NowDate);
		return CurrentTime;
	}
	/**
	 * 得到当前的日期,共10位
	 * 返回格式：yyyy-MM-dd
	 * @return String
	 */
	public static String getCurrentDate() {
		Date NowDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		CurrentDate = formatter.format(NowDate);
		return CurrentDate;
	}

	public static Date getDate() {
		String sDate=getCurrentTime2();
        SimpleDateFormat bartDateFormat =
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = bartDateFormat.parse(sDate);
            return date;
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }
	public static Date getDateForMonth() {
		String sDate=getCurrentTime2();
        SimpleDateFormat bartDateFormat =
        new SimpleDateFormat("yyyy-MM-00 00:00:00");
        try {
            Date date = bartDateFormat.parse(sDate);
            return date;
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }
	/**
	 * 得到当前日期加上某一个整数的日期，整数代表天数
	 * 输入参数：currentdate : String 格式 yyyy-MM-dd
	 * 			add_day		:  int
	 * 返回格式：yyyy-MM-dd
	 */
	public static String addDay(String currentdate,int add_day){
		GregorianCalendar gc=null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		int year,month,day;
		
		try {
			year=Integer.parseInt(currentdate.substring(0,4));
			month=Integer.parseInt(currentdate.substring(5,7))-1;
			day=Integer.parseInt(currentdate.substring(8,10));
			
			gc=new GregorianCalendar(year,month,day);
			gc.add(GregorianCalendar.DATE,add_day);
		
			return formatter.format(gc.getTime());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 得到当前日期加上某一个整数的日期，整数代表天数
	 * 输入参数：currentdate : String 格式 yyyy-MM-dd
	 * 			add_minutes		:  int
	 * 返回格式：yyyy-MM-dd
	 */
	public static String addMinutes(String currentdate,int add_minutes){
		GregorianCalendar gc=null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int year,month,day,hourOfDay,minute,second;
		
		try {
			year=Integer.parseInt(currentdate.substring(0,4));
			month=Integer.parseInt(currentdate.substring(5,7))-1;
			day=Integer.parseInt(currentdate.substring(8,10));
			hourOfDay=Integer.parseInt(currentdate.substring(11,13));
			minute=Integer.parseInt(currentdate.substring(14,16));
			second=Integer.parseInt(currentdate.substring(17,19));
			gc=new GregorianCalendar(year,month,day,hourOfDay,minute,second);
			gc.add(GregorianCalendar.MINUTE,add_minutes);
		
			return formatter.format(gc.getTime());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public static Date parseDay(String sDate) {
        SimpleDateFormat bartDateFormat =
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Date date = bartDateFormat.parse(sDate);
            return date;
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }
	public static Date parseDate(String sDate) {
        SimpleDateFormat bartDateFormat =
        new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = bartDateFormat.parse(sDate);
            return date;
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }
	public static Date parseDate2(String sDate) {
        SimpleDateFormat bartDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            Date date = bartDateFormat.parse(sDate);
            return date;
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }
	public static String getWeekDate(String currentdate,int weekDay){
		GregorianCalendar gc=null;
		Calendar calendar=Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		int year,month,day,wday;
		
		try {
			year=Integer.parseInt(currentdate.substring(0,4));
			month=Integer.parseInt(currentdate.substring(5,7))-1;
			day=Integer.parseInt(currentdate.substring(8,10));
			
			gc=new GregorianCalendar();

			//wday = gc.DATE;
			wday = calendar.get(Calendar.DAY_OF_WEEK)-1;
            
			if(wday==0)
				wday = 7;
			return addDay(currentdate,weekDay-wday);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	public static long getInterval(Date firstDate,Date secondDate){
		long interval=Math.abs((secondDate.getTime()-firstDate.getTime())/1000);
		return interval;
	}
	public static String getSecondsUnit(Long totalUpTime){
		String totalUpTimeUnit="0秒";
		if(null==totalUpTime||totalUpTime.longValue()==0)
			totalUpTimeUnit="0秒";
		else{
			if(totalUpTime.longValue()<60)
				totalUpTimeUnit=totalUpTime.longValue()+"秒";
			else if(totalUpTime.longValue()<3600){
				long minutes=totalUpTime.longValue()/60;
				totalUpTimeUnit=minutes+"分"+(totalUpTime.longValue()-60*minutes)+"秒";
			}
			else if(totalUpTime.longValue()>=3600){
				long hour=totalUpTime.longValue()/3600;
				long minus=totalUpTime.longValue()-hour*3600;
				long minutes=0;
				if(minus>=60)
					minutes=minus/60;
				long seconds=totalUpTime.longValue()-hour*3600-minutes*60;
				totalUpTimeUnit=hour+"小时"+minutes+"分钟"+seconds+"秒";
			}
		}
		return totalUpTimeUnit;
	}
	public static Date parstDate(Date date){
		String str=null;
		if(null!=date){
		SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		str = formatter.format(date);
		return TimeHelper.parseDay(str);
		}else
			return null;
		
	}
	public static void main(String[] args){
/*		long successfulTimes=100;
		long totalTimes=200;
		DecimalFormat a = new DecimalFormat("0.00%");
	    String ok=a.format((double)successfulTimes/(double)totalTimes);*/
		String sysDate="2010-12-31 23:45:59";
		//System.out.println(TimeHelper.getEndTime(sysDate));
		Long l = 90l;
		System.out.println(TimeHelper.getCurrentTime8());
	}
}
